//
//  PeiHuShiTableViewCell.m
//  XiangMu
//
//  Created by 康世标 on 15/7/21.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "PeiHuShiTableViewCell.h"

@implementation PeiHuShiTableViewCell

- (void)awakeFromNib {
    // Initialization code
    _hedImagVielw1.layer.masksToBounds=YES;
    [_hedImagVielw1.layer setCornerRadius:40];

}
- (void)shuJu:(NSMutableArray *)arr index:(NSInteger)index lat:(double)lat longitude:(double)longitude;
{
    self.latitude= lat;
    self.longitude = longitude;
    NSDictionary *dic=arr[index];
    
    self.name.text= [dic objectForKey:@"realname"]==[NSNull null]?@"":[NSString stringWithFormat:@"%@ :",[dic objectForKey:@"realname"]];
    
    [self.hedImagVielw1 sd_setImageWithURL:[NSURL URLWithString:[dic valueForKey:@"headImg"]==[NSNull null]?@"http://img5.duitang.com/uploads/item/201405/17/20140517133100_5SBvs.png":[dic valueForKey:@"headImg"]] placeholderImage:[UIImage imageNamed:@"like"]];
    //地址
    
    self.censusRegister.text = [NSString stringWithFormat:@"%@      护龄%@年",[dic valueForKey:@"censusRegister"]==[NSNull null]?@"":[dic valueForKey:@"censusRegister"],[dic valueForKey:@"carerAge"]==[NSNull null]?@"":[dic valueForKey:@"carerAge"]];
    //内容
    self.description1.text = [dic valueForKey:@"description"]==[NSNull null]?@"":[dic valueForKey:@"description"];
    self.price.text=[NSString stringWithFormat:@"￥%@元",[dic valueForKey:@"minPrice"]==[NSNull null]?@"":[dic valueForKey:@"minPrice"]];
    self.commentNum.text=[dic valueForKey:@"commentNum"]==[NSNull null]?@"":[NSString stringWithFormat:@"(%@)",[dic valueForKey:@"commentNum"]];
    //距离
    NSString *lid=[self JingWeiDu:[[dic valueForKey:@"latitude"] doubleValue] longitude:[[dic valueForKey:@"longitude"] doubleValue]];
    
    
    
    if ([lid intValue]<1000)
    {
        self.realPos.text = [NSString stringWithFormat:@"%d米",[lid intValue]];
    }
    else{
        self.realPos.text = [NSString stringWithFormat:@"%d千米",[lid intValue]/1000];
    }
    
    if ([[dic valueForKey:@"state"] intValue]==1)
    {
        //闲
        self.state.image = [UIImage imageNamed:@"idle"];
    }
    else{
        
        self.state.image = [UIImage imageNamed:@"busy"];
    }
    

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString * avg= [dic valueForKey:@"avg"] == [NSNull null]?@"":[dic valueForKey:@"avg"];
    switch ([avg intValue]) {
        case 0:
        {
            self.sumGrade1.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade2.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade3.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 1:
        {
            self.sumGrade1.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade2.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade3.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 2:
        {
            self.sumGrade1.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade2.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade3.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 3:
        {
            self.sumGrade1.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade2.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade3.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            self.sumGrade5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 4:
        {
            self.sumGrade1.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade2.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade3.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade4.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 5:
        {   self.sumGrade1.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade2.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade3.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade4.image = [UIImage imageNamed:@"wujiaoxing"];
            self.sumGrade5.image = [UIImage imageNamed:@"wujiaoxing"];
            
        }
            break;
            
        default:
            break;
    }

}
- (NSString *)JingWeiDu:(double )latitude longitude:(double)longitude {
    
    
    if (!latitude||!longitude) {
        
        return @"0";
    }
    
    CLLocation *orig=[[CLLocation alloc] initWithLatitude:_latitude  longitude:_longitude] ;
    
    
    CLLocation* dist=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude ] ;
    
    CLLocationDistance kilometers=[dist distanceFromLocation:orig];
    //         NSLog(@"距离:%f",kilometers);
    return [NSString stringWithFormat:@"%f",kilometers];
    
    
    
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
